﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.Objects;


namespace Client_Revenue
{
    public partial class Clients : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            //pnContact.Visible = false;
            //pnClientAddres.Visible = false;
        }

        #region "Init Grid Controls"
        private TextBox txtClientName, txtFirstName, txtMidName, txtLastName, txtWorkPhone, txtMobilePhone, txtEmailAddress;

        protected void txtClientName_Init(object sender, EventArgs e)
        {
            txtClientName = sender as TextBox;
        }
        protected void txtFirstName_Init(object sender, EventArgs e)
        {
            txtFirstName = sender as TextBox;
        }
        protected void txtMidName_Init(object sender, EventArgs e)
        {
            txtMidName = sender as TextBox;
        }
        protected void txtLastName_Init(object sender, EventArgs e)
        {
            txtLastName = sender as TextBox;
        }
        protected void txtWorkPhone_Init(object sender, EventArgs e)
        {
            txtWorkPhone = sender as TextBox;
        }
        protected void txtMobilePhone_Init(object sender, EventArgs e)
        {
            txtMobilePhone = sender as TextBox;
        }
        protected void txtEmailAddress_Init(object sender, EventArgs e)
        {
            txtEmailAddress = sender as TextBox;
        }
        #endregion

        protected void ClientsGridView_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Display ContactPersonInfo
            try
            {
                using (var DE = new dominiumEntities())
                {
                    ContactPersonGridView.DataSource = DE.GetClientContactPerson(int.Parse(ClientsGridView.SelectedValue.ToString()));
                    ContactPersonGridView.DataBind();
                }
            }
            catch (Exception ex)
            { throw ex; }
            //pnContact.Visible = true;
            //pnClientAddres.Visible = true;
        }


        protected void ClientsGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                GridViewRow gvr = ClientsGridView.Rows[ClientsGridView.EditIndex];
                using (var DE = new dominiumEntities())
                {
                    var ClientBeingUpdated = Convert.ToInt32(e.Keys[0]);
                    var theClient = (from c in DE.Clients
                                     where c.ClientId == ClientBeingUpdated
                                     select c).FirstOrDefault();

                    if (String.IsNullOrWhiteSpace(txtClientName.Text) == false)
                    {

                        theClient.ClientName = txtClientName.Text;
                    }
                    else
                    {
                        DE.DeleteObject(theClient);
                    }

                    DE.SaveChanges();

                }
            }
            catch (Exception ex)
            {
                e.Cancel = true;
                throw ex;

            }
        }
        protected void ContactPersonGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            try
            {
                using (var DE = new dominiumEntities())
                {
                    var PersonBeingUpdated = Convert.ToInt32(e.Keys["PersonId"]);
                    var theContact = (from p in DE.People
                                      where p.PersonId == PersonBeingUpdated
                                      select p).FirstOrDefault();

                    if (String.IsNullOrWhiteSpace(txtFirstName.Text) == false)
                    {

                        theContact.FirstName = txtFirstName.Text;
                        theContact.LastName = txtLastName.Text;
                        theContact.WorkPhone = txtWorkPhone.Text;
                        theContact.MobilePhone = txtMobilePhone.Text;
                        theContact.EmailAddress = txtEmailAddress.Text;
                    }
                    else
                    {
                        DE.DeleteObject(theContact);
                    }

                    DE.SaveChanges();

                    //re-bind contacts grid
                    ContactPersonGridView.DataBind();
                }
            }
            catch (Exception ex)
            {
                e.Cancel = true;
                throw ex;

            }
        }

        protected void ContactPersonGridView_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        protected void ContactPersonGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ContactPersonGridView.EditIndex = -1;
            ContactPersonGridView.DataBind();
        }

        protected void ClientsGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ClientsGridView.EditIndex = e.NewEditIndex;
            ClientsGridView.DataBind();
        }

        protected void ClientsGridView_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            ClientsGridView.EditIndex = -1;
            ClientsGridView.DataBind();
        }

        protected void ContactPersonGridView_RowEditing(object sender, GridViewEditEventArgs e)
        {
            ContactPersonGridView.EditIndex = e.NewEditIndex;
            ContactPersonGridView.DataBind();
        }


        protected void ContactPersonGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
 ContactPersonGridView.PageIndex = e.NewPageIndex;
            ContactPersonGridView.DataBind();
        }

       
    }
}